package com.example.order_product.Controller;


import com.example.order_product.Entity.Order;
import com.example.order_product.Entity.Product;
import com.example.order_product.Service.OrderService;
import com.example.order_product.Service.ProductService;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;

    private final ProductService productService;

    public OrderController(OrderService orderService, ProductService productService) {
        this.orderService = orderService;
        this.productService = productService;
    }


    //http://localhost:8080/order/createOrder
    @PostMapping("createOrder")
    @Transactional
    public ResponseEntity<?> createOrder(@Valid @RequestBody Order order,
                                         BindingResult result) {
        if (result.hasErrors()) {
            // 收集所有的错误信息
            List<String> errorMessages = result.getAllErrors().stream()
                    .map(error -> error.getDefaultMessage())
                    .collect(Collectors.toList());
            // 返回包含所有错误信息的响应
            return ResponseEntity.badRequest().body(errorMessages);
        }
        Product product = productService.findProductByOrder(order);
        if (product == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "没有此商品");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        if (orderService.judgeStock(order, product)) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "库存不足");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        orderService.addOrder(order, product);
        return ResponseEntity.ok(order);
    }

    @PutMapping("completeOrder")
    public ResponseEntity<?> completeOrder(int id) {
        Order order = orderService.findOrderByid(id);
        if (order == null) return ResponseEntity.notFound().build();
        Product product = productService.findProductByProductId(order.getProductId());
        if (product.getStock() < order.getQuantity()) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "库存不足");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        if (order.getStatus().equals("已完成")) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "该订单已完成");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        orderService.updateOrderStatus(order);
        return ResponseEntity.ok().build();
    }


}

